package leetcode.code2187;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public long minimumTime(int[] time, int totalTrips) {
		long min = Integer.MAX_VALUE;// 用int试一试，暴风哭泣
		for (int t : time) {
			min = Math.min(min, t);
		}
		int len = time.length;
		long r = totalTrips * min;
		long l = 1;
		long ans = 0;
		while (l <= r) {
			long m = l + ((r - l) >> 1);
			long cur = 0;// 用int试一试，暴风哭泣
			for (int i = 0; i < len; i++) {
				cur += m / time[i];
			}
			if (cur >= totalTrips) {
				ans = m;
				r = m - 1;
			} else {
				l = m + 1;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(100000000000L, this.minimumTime(HelpDebug.str2array3("[10000]"), 10000000));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(3, this.minimumTime(HelpDebug.str2array3("[1,2,3]"), 5));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
		HelpDebug.compare(2, this.minimumTime(HelpDebug.str2array3("[2]"), 1));

	}

	@Override
	public void debug4() {
		HelpDebug.compare(25, this.minimumTime(HelpDebug.str2array3("[5,10,10]"), 9));

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
